﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;
using EntityModel;

namespace PL___Windows_Inteface
{
    public partial class ManageBusiness : Form
    {
        private Business b;
        public ManageBusiness(String operation, string username)
        {
            InitializeComponent();
            if (operation == "ADD") {

                save_changes_bt.Visible = false;
                delete_bt.Visible = false;
            }
            else if (operation == "VIEW") {
                buttonSubmit.Visible = false;
                BL controller = new BL();
                try
                {
                    b = controller.getBusinessCustomer(username);
                }
                catch {
                    error_lb.Text = "Invalid Username";
                   }
                username_tb.Text = b.Customer.username;
                password_tb.Text = b.Customer.password;
                address_tb.Text = b.Customer.address_c;
                phone_tb.Text = b.Customer.phone.ToString();
                company_id_tb.Text = b.b_identifier;
                company_name_tb.Text = b.company_name;
                contact_person_tb.Text = b.contact_person;
                fax_tb.Text = b.fax.ToString(); 
            }
        }
        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            if (bt.Text == "Submit")
            {
                b = new Business();
                b.Customer = new Customer();
                b.Customer.typeCustumer = 2;
            }
            try
            {
                try
                {
                    b.fax = Convert.ToInt32(fax_tb.Text);
                }
                catch
                {
                    throw new Exception("FAX only have numbers");
                }
              
                b.Customer.address_c = address_tb.Text;
                try
                {
                    b.Customer.phone = Convert.ToInt32(phone_tb.Text);
                }
                catch
                {
                    throw new Exception("Telephone only have numbers");
                }
                b.Customer.username = username_tb.Text;
                b.Customer.password = password_tb.Text;
                b.b_identifier = company_id_tb.Text;
                b.company_name = company_name_tb.Text;
                b.contact_person = contact_person_tb.Text;
                
                BL controller = new BL();
                if (bt.Text == "Submit")
                {
                    controller.saveBusinessCustomer(b);
                    error_lb.ForeColor = Color.Blue;
                    error_lb.Text = "Customer Added Correctly";
                }
                else
                {
                    controller.updateBusinessCustomer(b);
                    error_lb.ForeColor = Color.Blue;
                    error_lb.Text = "Customer Updated Correctly";
                }






            }
            catch (Exception ex)
            {
                error_lb.Text = ex.Message;
            }

        }

        private void delete_bt_Click(object sender, EventArgs e)
        {
            BL controller = new BL();
            try
            {
                controller.deleteBusinessCustomer(b);
                MessageBox.Show("Customer Deleted Correctly", "Deleted", MessageBoxButtons.OK);
                this.Close();
            }
            catch (Exception ex)
            {
                error_lb.Text = ex.Message;

            }

        }


    }
}
